package ua.edu.onu.ua;

import java.io.*;

import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FillerFunctional{
    private static byte[] fileBuffer = new byte[1024];
    public static boolean isExist(File file) {
        return file.exists();}
    public static String getName(File file) {
        return file.getName();}
    public static long getSize(File file) {
        return file.length();}
    public static void write(File file, String text) throws IOException {
        BufferedWriter writeToFile = new BufferedWriter(new FileWriter(file));
        try {
            writeToFile.write(text + "\n");
            writeToFile.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            writeToFile.close();
        }}
    public static String read(File file) throws IOException {
        BufferedReader readFromFile = null;
        try {
            readFromFile = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            System.err.println("File no found");}
        return readFromFile.readLine();}
    public static void cleanFile(File file) throws IOException {
        BufferedWriter cleanFile = new BufferedWriter(new FileWriter(file));
        cleanFile.write("");
        cleanFile.flush();
        cleanFile.close();
    }
    public static void packZip(String[] filesToZip, String zipFileName) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(zipFileName));
            ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFileName));
            jos.setLevel(Deflater.DEFAULT_COMPRESSION);
            for (int i = 0; i < filesToZip.length; i++) {
                System.out.println(i);
                jos.putNextEntry(new ZipEntry(filesToZip[i]));
                FileInputStream in =  new FileInputStream(filesToZip[i]);
                int len;
                while ((len = in.read(fileBuffer)) > 0)
                    jos.write(fileBuffer, 0, len);
                jos.closeEntry();
                in.close();
            }
            jos.close();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println("Некорректный аргумент");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Файл не найден");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Ошибка доступа");
        }}
    public static void packZip(String folder, String name) throws Exception {
        byte[] buffer = new byte[18024];
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name));
        FileInputStream in = new FileInputStream(folder);
        out.putNextEntry(new ZipEntry(name));
        int len;
        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.closeEntry();
        in.close();
        out.close();}
    public void main (String[] args){
        try {
            packZip("tozip.doc","text.zip");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}